<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="com.engine.common.util.ServiceUtil" %>
<%@ page import="com.engine.workflow.constant.PAResponseCode" %>
<%@ page import="com.engine.workflow.entity.publicApi.PAResponseEntity" %>
<%@ page import="com.engine.workflow.entity.publicApi.ReqOperateRequestEntity" %>
<%@ page import="com.engine.workflow.entity.publicApi.WorkflowDetailTableInfoEntity" %>
<%@ page import="com.engine.workflow.entity.requestForm.ReqFlowFailMsgEntity" %>
<%@ page import="com.engine.workflow.publicApi.WorkflowRequestOperatePA" %>
<%@ page import="com.engine.workflow.publicApi.impl.WorkflowRequestOperatePAImpl" %>
<%@ page import="static weaver.common.DateUtil.getDateTime" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.workflow.webservices.WorkflowRequestTableField" %>
<%@ page import="weaver.workflow.webservices.WorkflowRequestTableRecord" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%--获取流水号--%>
<%
    JSONObject jsonData = new JSONObject();
    User user = HrmUserVarify.getUser(request,response);
    Map<String, String> mainMap = new HashMap<>();
    String formid = "16";
    mainMap.put("sqr","1");
    mainMap.put("sqbm","8");
    mainMap.put("bxje","100");
    mainMap.put("bxsy","测试使用");
    List<Map<String, Object>> detailList = new ArrayList<>();
    Map<String, Object> detail1Map = new HashMap<>();
    detail1Map.put("tablename","formtable_main_"+formid+"_dt1");
    List<Map<String, String>> field1List = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
    map.put("zsdz", "汉庭酒店");
    map.put("zsfy", "900");
    map.put("rs", "10");
    map.put("dw", "本单位");
    field1List.add(map);
    Map<String, String> map1 = new HashMap<>();
    map1.put("zsdz", "万达酒店");
    map1.put("zsfy", "200");
    map1.put("rs", "3");
    map1.put("dw", "外单位");
    field1List.add(map1);
    detail1Map.put("tablefield",field1List);
    detailList.add(detail1Map);

    Map<String, Object> detail2Map = new HashMap<>();
    detail2Map.put("tablename","formtable_main_"+formid+"_dt2");
    List<Map<String, String>> field2List = new ArrayList<>();
    Map<String, String> map2 = new HashMap<>();
    map2.put("gzdd", "北京");
    map2.put("gznr", "测试");
    map2.put("sjld", "1");
    map2.put("szbm", "10");
    field2List.add(map2);
    Map<String, String> map3 = new HashMap<>();
    map3.put("gzdd", "南京");
    map3.put("gznr", "测试2");
    map3.put("sjld", "1");
    map3.put("szbm", "10");
    field2List.add(map3);
    detail2Map.put("tablefield",field2List);
    detailList.add(detail2Map);

    Map<String, Object> resultMap = createWorkflowByRequestMap(user,formid,"0",mainMap,detailList);
    jsonData.put("resultMap", resultMap);
    out.print(jsonData.toJSONString());
%>
<%!
    private Map<String, Object> createWorkflowByRequestMap(User user, String formid, String isnextflow, Map<String, String> mainMap, List<Map<String, Object>> detailList) {
        //主表数据
        ReqOperateRequestEntity requestParam = new ReqOperateRequestEntity();
        List<WorkflowRequestTableField> mainData = new ArrayList<>();
        for (Map.Entry<String, String> entry : mainMap.entrySet()) {
            WorkflowRequestTableField field = new WorkflowRequestTableField();
            field.setFieldName(entry.getKey());
            field.setFieldValue(entry.getValue());
            mainData.add(field);
        }
        requestParam.setMainData(mainData);
        //明细表数据
        if (!"".equals(Util.null2String(detailList))) {
            List<WorkflowDetailTableInfoEntity> detailData = new ArrayList<>();
            for (Map<String, Object> dtlmap : detailList) {
                String tablename = Util.null2String(dtlmap.get("tablename"));
                List<Map<String, String>> tableFieldList = (List<Map<String, String>>) dtlmap.get("tablefield");
                //设置明细信息
                WorkflowDetailTableInfoEntity details = new WorkflowDetailTableInfoEntity();
                details.setTableDBName(tablename);
                //明细数据(多少条明细)
                WorkflowRequestTableRecord[] detailRows = new WorkflowRequestTableRecord[tableFieldList.size()];
                //遍历每一行明细
                int detailRowsIndex = 0;
                for (Map<String, String> map : tableFieldList) {
                    //明细行数据（每行有多少个字段）
                    WorkflowRequestTableField[] rowDatas = new WorkflowRequestTableField[map.size()];
                    int rowDatasIndex = 0;
                    //遍历明细每个字段
                    for (Map.Entry<String, String> detailMap : map.entrySet()) {
                        //行字段数据 （设置每个字段具体值）
                        WorkflowRequestTableField fields = new WorkflowRequestTableField();
                        fields.setFieldName(detailMap.getKey());
                        fields.setFieldValue(detailMap.getValue());
                        rowDatas[rowDatasIndex++] = fields;
                    }        //封装某行明细数据
                    WorkflowRequestTableRecord rows = new WorkflowRequestTableRecord();
                    rows.setRecordOrder(0);
                    rows.setWorkflowRequestTableFields(rowDatas);
                    detailRows[detailRowsIndex++] = rows;
                }
                //封装所有明细数据
                details.setWorkflowRequestTableRecords(detailRows);
                detailData.add(details);
                requestParam.setDetailData(detailData);
            }
        }
        Map<String, Object> otherParams = new HashMap<>();
        otherParams.put("isnextflow", isnextflow);
        requestParam.setOtherParams(otherParams);
        //固定创建流程id
        Map<String, String> wf = getWorkflowId(formid);
        requestParam.setWorkflowId(Util.getIntValue(wf.get("workflowid")));
        requestParam.setRequestName(Util.null2String(wf.get("workflowname")) + "-" + user.getUsername() + "-" + getDateTime());
        WorkflowRequestOperatePA operatePA = ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
        PAResponseEntity paResponseEntity = operatePA.doCreateRequest(user, requestParam);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.putAll((Map<String, Object>) paResponseEntity.getData());
        PAResponseCode paResponseCode = paResponseEntity.getCode();
        resultMap.put("message", paResponseCode.getMessage());
        resultMap.put("statuscode", paResponseCode.getStatusCode() + "");
        ReqFlowFailMsgEntity flowFailMsgEntity = paResponseEntity.getReqFailMsg();
        resultMap.putAll(paResponseEntity.getErrMsg());
        resultMap.putAll(flowFailMsgEntity.getMsgInfo());
        return resultMap;
    }

    private Map<String, String> getWorkflowId(String formid) {
        RecordSet rs = new RecordSet();
        Map<String, String> resultMap = new HashMap<>();
        rs.executeQuery("select (case when activeversionid is null then id else activeversionid end) from workflow_base " +
                "where abs(formid)=? group by (case when activeversionid is null then id else activeversionid end) ", formid);
        rs.next();
        String workflowid = Util.null2String(rs.getString(1));
        resultMap.put("workflowid", workflowid);
        rs.executeQuery("select workflowname from workflow_base where id=?", workflowid);
        rs.next();
        String workflowname = Util.null2String(rs.getString(1));
        resultMap.put("workflowname", workflowname);
        return resultMap;
    }


%>